//
//  BaseNavigationController.m
//  CBT
//
//  Created by 四点钟 on 2018/11/15.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "BaseNavigationController.h"


#define DECLARE_NAVIGATION_HIDE_LINE() -(void)useMethodToFindBlackLineAndHind{\
    UINavigationBar *bar = self.navigationBar;\
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];\
    blackLineImageView.hidden = YES;\
    UIView *shadow = [[UIView alloc] init];\
    shadow.frame = CGRectMake(0, CGRectGetHeight(bar.frame), CGRectGetWidth(bar.frame), 0.5f);\
    shadow.backgroundColor = UIColorFromRGB(0xe6e6e6);\
    [bar addSubview:shadow];\
}\
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view{\
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0){\
        return (UIImageView *)view;\
    }\
    for (UIView *subview in view.subviews) {\
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];\
        if (imageView) {\
            return imageView;\
        }\
    }\
return nil;\
}\

#define HIDE_NAVIGATION_LINE()     [self useMethodToFindBlackLineAndHind];

@interface BaseNavigationController ()

@end

@implementation BaseNavigationController

DECLARE_NAVIGATION_HIDE_LINE()

+ (void)initialize{
    
    UINavigationBar *bar = [UINavigationBar appearance];
    [[UINavigationBar appearance] setBackgroundImage:[[UIImage imageNamed:@"daohanglan_tongyong"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0) resizingMode:UIImageResizingModeStretch] forBarMetrics:UIBarMetricsDefault];
    
    [bar setTintColor:UIColorFromRGB(0x7865fb)];
    [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],
                                                                      NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Bold" size:20]}];
    [bar setBarTintColor:[UIColor clearColor]];
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    HIDE_NAVIGATION_LINE()
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark --
#pragma mark -- 状态栏
- (BOOL)shouldAutorotate {
    return [self.topViewController shouldAutorotate];
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}
- (UIViewController *)childViewControllerForStatusBarStyle {
    return self.topViewController;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    [HFStatusView dismissStatusView];
    
    if (self.viewControllers.count > 0){
        
        UIButton *button = [[UIButton alloc] init];
        [button setImage:[UIImage imageNamed:@"tongyong_fanhui"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        button.frame=CGRectMake(0, 0, 50, StartViewHeight);
        //[button sizeToFit];
        button.backgroundColor=[UIColor clearColor];
        [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        
        viewController.hidesBottomBarWhenPushed = YES;
    }
   
    [super pushViewController:viewController animated:animated];
    
    if (@available(iOS 11.0, *)){
        // 修改tabBra的frame
        CGRect frame = self.tabBarController.tabBar.frame;
        frame.origin.y = [UIScreen mainScreen].bounds.size.height - frame.size.height;
        self.tabBarController.tabBar.frame = frame;
    }
}

- (void)back{
//    [HFStatusView dismissStatusView];
    // 判断两种情况: push 和 present
    if ((self.presentedViewController || self.presentingViewController) && self.childViewControllers.count == 1) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else
        [self popViewControllerAnimated:YES];
}
@end
